package example2;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.util.*;

public class ListFileTest {
    @Test
    public void listAllFiles() throws Exception {

        Map<String, String> mapF = getFileListToMap(FileUtils.getFile("F:/"));
        Map<String, String> mapG = getFileListToMap(FileUtils.getFile("G:/"));

        List<String> list = new LinkedList<>();
        for (String key : mapF.keySet()) {
            if (mapG.containsKey(key)) {
                String format = String.format("%s,%s,%s", key, mapF.get(key), mapG.get(key));
                //System.out.println(format);
                list.add(format);
            }
        }
        System.out.println(list.size());
        File file = FileUtils.getFile("C:\\Users\\yangyang\\Desktop\\笔记文件.csv");
        FileUtils.writeStringToFile(file, StringUtils.join(list, "\n"), "GBK");
    }


    public Map<String, String> getFileListToMap(File file) {
        //递归获取所有文件和目录
        Collection<File> filesAndDirs = FileUtils.listFilesAndDirs(file, TrueFileFilter.INSTANCE, DirectoryFileFilter.DIRECTORY);
        Map<String, String> map = new HashMap<>();
        for (File dir : filesAndDirs) map.put(dir.getName(), dir.getAbsolutePath());
        return map;
    }


}
